<template>
  <div>
    <div class="search">
      <el-form :inline="true" :model="form" ref="formRef">
        <el-form-item label="班组名称" style="margin-left: 10px">
          <el-input v-model="teamsName" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="承包人" style="margin-left: 10px">
          <el-input v-model="contractor" placeholder="请输入"></el-input>
        </el-form-item>
<!--        <el-form-item label="班组人数" style="margin-left: 10px">-->
<!--          <el-input v-model="teamsNumber" placeholder="请输入"></el-input>-->
<!--        </el-form-item>-->
        <el-form-item>
          <el-button type="info" plain style="margin-left: 10px" @click="load(1)">查询</el-button>
          <el-button type="warning" plain style="margin-left: 10px" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="operation">
      <el-button type="primary" plain @click="handleAdd">新增</el-button>
    </div>

    <div class="table">
      <el-table :data="tableData" stripe border @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="teamsId" label="序号" width="80" align="center" sortable></el-table-column>
        <el-table-column prop="teamsName" label="班组名称" show-overflow-tooltip>
          <!--          设置班组名称那一列的数据点击跳转-->
          <!-- <template slot-scope="scope">
            <router-link to="/clickTeam">{{ scope.row.teamsName }}</router-link>
          </template> -->
          <template slot-scope="scope">
            <el-link type="primary" plain @click="handleEdit1(scope.row)">{{ scope.row.teamsName }}</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="jobs" label="工种" show-overflow-tooltip></el-table-column>
        <el-table-column prop="contractor" label="承包人"></el-table-column>
        <el-table-column prop="contractorPhone" label="联系方式"></el-table-column>
        <el-table-column prop="unitName" label="参与单位"></el-table-column>
        <el-table-column prop="projectName" label="对应项目"></el-table-column>
        <el-table-column prop="teamsNumber" label="班组人数"></el-table-column>
        <el-table-column label="操作" align="center" width="180">
          <template v-slot="scope">
            <el-button size="mini" type="primary" plain @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" plain @click="del(scope.row.teamsId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>


      <el-dialog title="班组信息" :visible.sync="fromVisible" width="40%" :close-on-click-modal="false" destroy-on-close>
        <el-form label-width="100px" style="padding-right: 50px" :model="form" :rules="rules" ref="formRef">
          <el-form-item prop="unitId" label="参与单位">
              <el-input v-model="form.unitId" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="teamsName" label="班组名称">
            <el-input v-model="form.teamsName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="jobs" label="工种">
              <el-input v-model="form.jobs" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="contractor" label="承包人">
            <el-input v-model="form.contractor" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="contractorPhone" label="联系方式">
            <el-input v-model="form.contractorPhone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="projectName" label="对应项目">
              <el-input v-model="form.projectName" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="fromVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </div>
      </el-dialog>

      <!-- 分页区域-->
      <div class="pagination">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageNum"
          :page-sizes="[1, 3, 5, 10]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>

  </div>
</template>

<script>
export default {
  name: "Department",
  data() {
    return {
      tableData: [],  // 所有的数据   
      pageNum: 1,   // 当前的页码
      pageSize: 10,  // 每页显示的个数
      total: 0,
      teamsName: null,
      contractor: null,
      teamsNumber: null,
      fromVisible: false,
      form: {},
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      rules: {
          unitId: [
          { required: true, message: '请输入单位id', trigger: 'blur' },
        ],
        teamsName: [
          { required: true, message: '请输入班组名称', trigger: 'blur' },
        ],
        jobs: [
          { required: true, message: '请输入工种', trigger: 'blur' },
        ],
        contractor: [
          { required: true, message: '请输入承包人', trigger: 'blur' },
        ],
        contractorPhone: [
          { required: true, message: '请输入联系方式', trigger: 'blur' },
        ],
        projectName: [
          { required: true, message: '请输入对应项目', trigger: 'blur' },
        ]
      },
      ids: [],
      // departmentData: [],
    }
  },
  created() {
    this.load(1)
    // this.loadDepartments()
  },
  methods: {
    handleAdd() {   // 新增数据
      this.form = {}  // 新增数据的时候清空数据
      this.fromVisible = "true"
    },
    handleEdit(row) {   // 编辑数据
      this.form = JSON.parse(JSON.stringify(row))  // 给form对象赋值  注意要深拷贝数据
      this.fromVisible = "true"
    },
    handleEdit1(row) {   // 编辑数据
      this.form = JSON.parse(JSON.stringify(row))  
      this.$router.push({
        name: "ClickTeam",
        query: { param: this.form },
      })
    },
    //监听pagesize改变的事件
    handleSizeChange(val) {
      this.pageSize = val
      this.loadDepartments()
    },
    handleCurrentChange(pageNum) {
      this.load(pageNum)
    },
    save() {   // 保存按钮触发的逻辑  它会触发新增或者更新
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.$request({
            url: this.form.unitId ? '/teamsinfo' : '/teams/insert',
            method: this.form.unitId ? 'PUT' : 'POST',
            data: this.form
          }).then(res => {
            if (res.code === '200') {  // 表示成功保存
              this.$message.success('保存成功')
              this.load(1)
              this.fromVisible = false
            } else {
              this.$message.error(res.msg)  // 弹出错误的信息
            }
          })
        }
      })
    },
    del(id) {   // 单个删除
      this.$confirm('您确定删除吗？', '确认删除', { type: "warning" }).then(response => {
        this.$request.delete('/teams/' + id).then(res => {
          if (res.code === '200') {   // 表示操作成功
            this.$message.success('操作成功')
            this.load(1)
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      }).catch(() => {
      })
    },
    handleSelectionChange(rows) {   // 当前选中的所有的行数据
      this.ids = rows.map(v => v.id)   //  [1,2]
    },
    load(pageNum) {  // 分页查询
      if (pageNum) this.pageNum = pageNum
      this.$request.get('/teams', {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          teamsName: this.teamsName,
          contractor: this.contractor,
          teamsNumber: this.teamsNumber,
        }
      }).then(res => {
        this.tableData = res.data.rows
        this.total = res.data?.total
      })
    },
    reset() {
      this.teamsName = null
      this.contractor = null
      this.teamsNumber = null
      this.load(1)
    },
  }
}
</script>

<style scoped></style>
